<?php

namespace app\store\model\user;

use app\store\model\business\User as BusinessUserModel;
use app\common\model\user\BusinessApply as BusinessApplyModel;

use app\store\model\User as UserModel;

/**
 * 用户开通商户后台申请模型
 * Class BusinessApply
 * @package app\store\model\user
 */
class BusinessApply extends BusinessApplyModel
{
    /**
     * 获取列表记录
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($search = '')
    {
         $this->alias('apply')
            ->field('apply.*')
            ->with(['user'])
            ->join('user', 'user.user_id = apply.user_id')
            ->order(['apply.apply_time' => 'desc']);
        !empty($search) && $this->where('user.nickName|apply.organ_name|apply.mobile|apply.contacts', 'like', "%$search%");
        // 获取列表数据
        return $this->paginate(15, false, [
            'query' => \request()->request()
        ]);
    }

    /**
     * 开通商户审核
     * @param $data
     * @return bool
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function submit($data)
    {
        if ($data['apply_status'] == '30' && empty($data['reject_reason'])) {
            $this->error = '请填写驳回原因';
            return false;
        }
        $this->startTrans();
        if ($data['apply_status'] == '20') {
            // 新增商户商用户
            $user_business = new BusinessUserModel();
            $user_business->createBusiness([
                'real_name'=>$this->contacts,
                'business_user_id'=>$this->user_id,
                'user_name'=>'admin',
                'password'=>'yohoo',
                'password_confirm'=>'yohoo',]);
        }
        // 更新申请记录
        $data['audit_time'] = time();
        $this->allowField(true)->save($data);
        // 发送模板消息
        //(new Message)->business($this);
        $this->commit();
        return true;
    }

    /**
     * 软删除
     * @return false|int
     */
    public function setDelete()
    {
        // 判断该等级下是否存在会员
        if (UserModel::checkExistByGradeId($this['grade_id'])) {
            $this->error = '该会员等级下存在用户，不允许删除';
            return false;
        }
        return $this->save(['is_delete' => 1]);
    }

    /**
     * 表单验证
     * @param $data
     * @param string $scene
     * @return bool
     */
    private function validateForm($data, $scene = 'add')
    {
        if ($scene === 'add') {
            // 需要判断等级权重是否已存在
            if (self::checkExistByWeight($data['weight'])) {
                $this->error = '等级权重已存在';
                return false;
            }
        } elseif ($scene === 'edit') {
            // 需要判断等级权重是否已存在
            if (self::checkExistByWeight($data['weight'], $this['grade_id'])) {
                $this->error = '等级权重已存在';
                return false;
            }
        }
        return true;
    }


}